Grokking Python Fundamentals
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Course Overview
Python Basics
Python Overview
Python Application Areas
Your First Python Program
Python - Basic Syntax
Python Comments
Quiz
Python Variables and Operators
Python - Variables
Python - Data types
Python - Numbers
Python - Booleans
Python - Type Conversions
Python - Operators
Quiz
Python Conditionals
Introduction to Conditional Statements in Python
Python - If...Else
Python - Match Case
Quiz
Python loops
Introduction to loops in Python
Python - for loop
Python - while loop
Python - Loop Control
Python - for else and while else loop
Quiz
Python Strings
Introduction to Strings in Python
Python - String Operators
Python Slicing Strings
Python - String Methods
Quiz
Python Array
Introduction to Array in Python
Python - Array Methods
Quiz
Python Lists
Introduction to Lists in Python
Python - Add and Remove List Items
Python - List Comprehension
Python - List Methods
Quiz
Introduction to Tuple in Python
Python - Unpack Tuple Items
Python - Join Tuples
Python - Tuple Methods
Quiz
Introduction to Dictionary in Python
Python - Nested Dictionaries
Python - Dictionary Methods
Quiz
Introduction to set in Python
Python - set Operations
Python - Set Methods
Quiz
Introductions to Function in Python
Python - Function Arguments
Recursive Functions
Python - Variable scope
Importing Modules
Quiz
Python - List Comprehension

List comprehension in Python is a powerful and concise way to create lists by performing operations on existing lists or other iterable collections. This feature provides a more succinct and efficient means to generate lists compared to traditional loops and conditional logic.

Why We Need List Comprehension?

Traditionally, to create a new list from an existing one, you would use a for-loop to iterate through the elements, apply a transformation or condition, and append the result to a new list. This method can be verbose and less efficient, especially with large data sets.

Example with Traditional Approach

Python3

. . . .

Issues with Traditional Approach:

  • Verbose: Multiple lines are needed to perform a simple transformation.
  • Inefficient: The append operation within a loop can slow down execution for large lists.

List Comprehension

List comprehension condenses the process of iterating over an iterable and applying expressions into a single, readable line. This not only cleans up the code but can also enhance performance.

Syntax of List Comprehension

Image
Python3
. . . .

Explanation:

  • expression: The output expression producing elements of the new list from the items of the iterable (e.g., item**2).
  • for item in iterable: A for-loop over any iterable (like a list, tuple, or string) that serves as the source.
  • if condition: An optional conditional to filter items from the iterable.

Example 1: Simple List Comprehension

Creating a list of squared numbers from an existing list.

Python3

. . . .

Explanation:

  • numbers = [1, 2, 3, 4, 5]: Defines the original list of numbers.
  • [number ** 2 for number in numbers]: Squares each number in numbers and collects the results into a new list.
  • The print statement outputs the squared numbers.

Example 2: Conditional List Comprehension

Creating a list of squared numbers but only for even numbers.

Python3

. . . .

Explanation:

  • if number % 2 == 0: Filters the list to include only even numbers.
  • The expression number ** 2 is executed only for numbers that satisfy the condition.

Example 3: Complex Expressions

Generating a formatted string list using numbers.

Python3

. . . .

Explanation:

  • Each element in numbers is formatted into a string that describes the number and its square.
  • The list comprehension includes a formatted string as the expression.

List comprehension simplifies the creation and manipulation of lists in Python by reducing the complexity and verbosity of loops and conditional logic. It is not only syntactically elegant but also computationally efficient, making it an essential tool for Python developers engaged in data manipulation and processing tasks.

Python - Add and Remove List Items
Python - List Methods
Mark as Completed

On this page

Why We Need List Comprehension?

Example with Traditional Approach

List Comprehension

Syntax of List Comprehension

Example 1: Simple List Comprehension

Example 2: Conditional List Comprehension

Example 3: Complex Expressions